package com.weaver.qfengx.api.ding;

import com.alibaba.fastjson.JSONObject;
import com.weaver.qfengx.HttpClientUtils;
import com.weaver.qfengx.StringUtils;
import com.weaver.qfengx.api.ding.config.DingConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 钉钉HttpApi接口HttpClient版本
 */
public class DingApiServiceHttpClient {

    private String host = "";
    private CloseableHttpClient client;

    public DingApiServiceHttpClient(CloseableHttpClient client, String host) {
        this.client = client;
        this.host = host;
    }

    public DingApiServiceHttpClient(String host) {
        this.client = HttpClients.createDefault();
        this.host = host;
    }

    public DingApiHttpClientResult getToken(String appkey, String appsecret) throws Exception {
        Map<String, String> reqParam = new HashMap<>();
        reqParam.put("appkey", appkey);
        reqParam.put("appsecret", appsecret);
        CloseableHttpResponse response = HttpClientUtils.doGet(this.client, this.host + DingConfig.getToken, reqParam);
        DingApiHttpClientResult result = new DingApiHttpClientResult().setHttpcode(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            String respBody = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(respBody);
            result.setErrcode(jsonObject.getInteger("errcode")).setErrmsg(jsonObject.getString("errmsg"));
            if (StringUtils.equals("0", jsonObject.getString("errcode"))) {
                return result.setAccess_token(jsonObject.getString("access_token"));
            }
            return result;
        } else {
            return result.setErrcode(-1).setErrmsg("请求异常");
        }
    }

    // 获取服务号列表
    public DingApiHttpClientResult serviceaccountList(String token, int pageStart, int pageSize) throws Exception {
        // body体内容
        Map<String, Object> reqParam = new HashMap<>();
        reqParam.put("pageStart", pageStart);
        reqParam.put("pageSize", pageSize);
        CloseableHttpResponse response = HttpClientUtils.doPostByJson(this.client, this.host + DingConfig.serviceaccountList + "?access_token=" + token, reqParam);
        DingApiHttpClientResult result = new DingApiHttpClientResult().setHttpcode(response.getStatusLine().getStatusCode()).setAccess_token(token);
        if (response.getStatusLine().getStatusCode() == 200) {
            String respBody = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(respBody);
            result.setErrcode(jsonObject.getInteger("errcode")).setErrmsg(jsonObject.getString("errmsg")).setRequest_id(jsonObject.getString("request_id"));
            if (StringUtils.equals("0", jsonObject.getString("errcode"))) {
                return result.setItem_count(jsonObject.getInteger("item_count")).setJsonArray(jsonObject.getJSONArray("items"));
            }
            return result;
        } else {
            return result.setErrcode(-1).setErrmsg("请求异常");
        }
    }

    // 消息群发
    public DingApiHttpClientResult massSend(String token, String unionid, String media_id,
                                                   boolean is_to_all, String msg_type, String text_content,
                                                   String uuid, String userid_list, String dep_id_list,
                                                   Boolean is_preview, Map<String, Object> msg_body) throws Exception {
        Map<String, Object> reqParam = new HashMap<>();
        reqParam.put("unionid", unionid);
        if (StringUtils.isNotEmpty(media_id)) {
            reqParam.put("media_id", media_id);
        }
        reqParam.put("is_to_all", is_to_all);
        reqParam.put("msg_type", msg_type);
        if (StringUtils.isNotEmpty(text_content)) {
            reqParam.put("text_content", text_content);
        }
        reqParam.put("uuid", uuid);
        if (StringUtils.isNotEmpty(userid_list)) {
            reqParam.put("userid_list", userid_list);
        }
        if (StringUtils.isNotEmpty(dep_id_list)) {
            reqParam.put("dep_id_list", dep_id_list);
        }
        if (is_preview != null) {
            reqParam.put("is_preview", is_preview);
        }
        if (msg_body != null && !msg_body.isEmpty()) {
            reqParam.put("msg_body", msg_body);
        }
        CloseableHttpResponse response = HttpClientUtils.doPostByJson(this.client, this.host + DingConfig.massSend + "?access_token=" + token, reqParam);
        DingApiHttpClientResult result = new DingApiHttpClientResult().setHttpcode(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            String respBody = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(respBody);
            result.setErrcode(jsonObject.getInteger("errcode")).setErrmsg(jsonObject.getString("errmsg")).setRequest_id("request_id");
            if (StringUtils.equals("0", jsonObject.getString("errcode"))) {
                return result.setTask_id(jsonObject.getString("task_id"));
            }
            return result;
        } else {
            return result.setErrcode(-1).setErrmsg("请求异常");
        }
    }

}
